package com.example.voiceassistant.config;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Properties;

/**
 * 配置管理器，负责提供系统运行所需的基础参数。
 * 演示实现中直接读取内置默认值，真实环境可接入配置中心或数据库。
 */
public class ConfigManager {

    private final Properties properties = new Properties();

    public ConfigManager() {
        loadDefaults();
    }

    private void loadDefaults() {
        properties.put("wake.words", "AI管家,AI助手");
        properties.put("demo.audio.frames", ""); // 移除模拟，切换真实
        properties.put("demo.audio.interval.ms", "500");
        properties.put("dialog.timeout.seconds", "60");
        properties.put("ai.model", "mPangu-demo");
        properties.put("ai.apikey", "ms-23b03747-89a3-4e6c-bd98-6b570db5c563"); // 用户需替换真实魔搭 Key
        properties.put("ai.endpoint", "https://api.modelscope.cn/api/v1/tasks/qwen2.5-coder-7b-instruct"); // 示例 AI
        properties.put("asr.endpoint", "https://api-inference.modelscope.cn/v1/speech/asr"); // 魔搭 ASR 示例
        properties.put("asr.apikey", "ms-23b03747-89a3-4e6c-bd98-6b570db5c563"); // ASR Key，同 AI 或单独
        properties.put("asr.model", "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch");
        properties.put("locale", Locale.SIMPLIFIED_CHINESE.toLanguageTag());
    }

    public List<String> getWakeWords() {
        String raw = properties.getProperty("wake.words", "");
        if (raw.isBlank()) {
            return Collections.emptyList();
        }
        return Arrays.stream(raw.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .toList();
    }

    public List<String> getDemoAudioFrames() {
        String raw = properties.getProperty("demo.audio.frames", "");
        if (raw.isBlank()) {
            return Collections.emptyList();
        }
        return Arrays.stream(raw.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .toList();
    }

    public Duration getDemoAudioFrameInterval() {
        long millis = Long.parseLong(properties.getProperty("demo.audio.interval.ms", "1000"));
        return Duration.ofMillis(millis);
    }

    public int getDialogTimeoutSeconds() {
        return Integer.parseInt(properties.getProperty("dialog.timeout.seconds", "60"));
    }

    public String getAiModel() {
        return properties.getProperty("ai.model", "default");
    }

    public String getAiApiKey() {
        return properties.getProperty("ai.apikey", "");
    }

    public String getAiEndpoint() {
        return properties.getProperty("ai.endpoint", "");
    }

    public int getDemoRunSeconds() {
        return Integer.parseInt(properties.getProperty("demo.run.seconds", "30"));
    }

    public Optional<String> findProperty(String key) {
        return Optional.ofNullable(properties.getProperty(key));
    }

    public String getLocale() {
        return properties.getProperty("locale", Locale.SIMPLIFIED_CHINESE.toLanguageTag());
    }

    public String getAsrEndpoint() {
        return properties.getProperty("asr.endpoint", "");
    }

    public String getAsrApiKey() {
        return properties.getProperty("asr.apikey", "");
    }

    public String getAsrModel() {
        return properties.getProperty("asr.model", "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch");
    }
}

